package com.yh.hljapp.gddb.gbmanager;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.yh.hljapp.AppContext;
import com.yh.hljapp.gddb.gdbean.DaoMaster;
import com.yh.hljapp.gddb.gdbean.DaoSession;
import com.yh.hljapp.gddb.gdbean.MatBeanCount;
import com.yh.hljapp.gddb.gdbean.MatBeanCountDao;
import com.yh.hljapp.modules.bean.CustPhone;

import java.util.ArrayList;
import java.util.List;

/**
 * =========================
 * 版权所有，违法必究
 * 作者：john
 * <p>
 * 工程：FoodBarcode
 * <p>
 * 文件名：MatManager
 * <p>
 * 创建时间：2017.05.23.15:05
 * *****************************
 */
public class MatCountManager {
    private final static String dbName = "fb_db";
    private static MatCountManager mInstance;
    private DaoMaster.DevOpenHelper openHelper;
    private Context context;
    private final DaoSession mDaoSession;

    public MatCountManager(Context context) {
        this.context = context;
        openHelper = new DaoMaster.DevOpenHelper(context, dbName, null);
        DaoMaster daoMaster = new DaoMaster(getWritableDatabase());
        mDaoSession = daoMaster.newSession();
    }

    /**
     * 获取单例引用
     *
     * @param context
     * @return
     */
    public static MatCountManager getInstance(Context context) {
        if (mInstance == null) {
            synchronized (MatCountManager.class) {
                if (mInstance == null) {
                    mInstance = new MatCountManager(context);
                }
            }
        }
        return mInstance;
    }

    /**
     * 获取可读数据库
     */
    private SQLiteDatabase getReadableDatabase() {
        if (openHelper == null) {
            openHelper = new DaoMaster.DevOpenHelper(context, dbName, null);
        }
        SQLiteDatabase db = openHelper.getReadableDatabase();
        return db;
    }

    /**
     * 获取可写数据库
     */
    private SQLiteDatabase getWritableDatabase() {
        if (openHelper == null) {
            openHelper = new DaoMaster.DevOpenHelper(context, dbName, null);
        }
        SQLiteDatabase db = openHelper.getWritableDatabase();
        return db;
    }

    //插入一个
    public void insertMat(MatBeanCount itemListBean) {
        String name = AppContext.getInstance().getCurrentUser().getName();
        itemListBean.setUserName(name);
        MatBeanCountDao matBeanDao = mDaoSession.getMatBeanCountDao();
        long insert = matBeanDao.insert(itemListBean);
    }

    //插入集合
    public boolean insertMatList(List<MatBeanCount> mbs) {
        MatBeanCountDao matBeanDao = mDaoSession.getMatBeanCountDao();
        try {
            matBeanDao.insertOrReplaceInTx(mbs);
        } catch (Exception e) {
            return false;
        }
        return true;

    }

    /**
     * 通过type拿到对应类型的条目总数  0代表商品1代表赠品
     * @param type
     * @return
     */
    public int getMatCountByType(String type) {
        String userName = AppContext.getInstance().getCurrentUser().getName();
        MatBeanCountDao matBeanDao = mDaoSession.getMatBeanCountDao();
        int size = matBeanDao.queryBuilder().where(MatBeanCountDao.Properties.Type.eq(type),MatBeanCountDao.Properties.UserName.eq(userName)).list().size();
        return size;
    }

    public void updateFHCKByPos(String type,String position,CustPhone fhckbean){
        String userName = AppContext.getInstance().getCurrentUser().getName();
        MatBeanCountDao matBeanDao = mDaoSession.getMatBeanCountDao();
        MatBeanCount unique = matBeanDao.queryBuilder().where(MatBeanCountDao.Properties.Type.eq(type), MatBeanCountDao.Properties.Posotion.eq(position), MatBeanCountDao.Properties.UserName.eq(userName)).unique();
        unique.setFDCStockID(fhckbean.getId());
        unique.setFDCStockName(fhckbean.getName());
        matBeanDao.update(unique);
    }

    //判断扫描的出库单是否存在
    public boolean isMatExist(String mat) {
        MatBeanCountDao matBeanDao = mDaoSession.getMatBeanCountDao();
        MatBeanCount unique = matBeanDao.queryBuilder().where(MatBeanCountDao.Properties.Id.eq(mat)).unique();
        if (unique == null) {
            return false;
        } else {
            return true;
        }
    }
    //更新count
    public void updateCount(String matId, String type, String count) {
        String userName = AppContext.getInstance().getCurrentUser().getName();
        MatBeanCountDao matBeanDao = mDaoSession.getMatBeanCountDao();
        MatBeanCount unique = matBeanDao.queryBuilder().where(MatBeanCountDao.Properties.Id.eq(matId),MatBeanCountDao.Properties.Type.eq(type),MatBeanCountDao.Properties.UserName.eq(userName)).unique();
        if (unique == null) {

        } else {
            unique.setCount(count);
            matBeanDao.update(unique);
        }
    }

    //删除所有数据
    public void delAllMat(){
        String name = AppContext.getInstance().getCurrentUser().getName();
        MatBeanCountDao matBeanDao = mDaoSession.getMatBeanCountDao();
        List<MatBeanCount> list = matBeanDao.queryBuilder().where(MatBeanCountDao.Properties.UserName.eq(name)).list();
        matBeanDao.deleteInTx(list);
    }

    //拿到所有数据
    public ArrayList<MatBeanCount> getAllMats(){
        String userName = AppContext.getInstance().getCurrentUser().getName();
        ArrayList<MatBeanCount> matBeen = (ArrayList<MatBeanCount>) mDaoSession.getMatBeanCountDao().queryBuilder().where(MatBeanCountDao.Properties.UserName.eq(userName)).list();
        return matBeen;


//        ArrayList<MatBeanCount> matBeen = (ArrayList<MatBeanCount>) mDaoSession.getMatBeanCountDao().loadAll();
//        return matBeen;
    }

    //根据类型拿到所有数据
    public ArrayList<MatBeanCount> getAllMatsByType(String type,String headId){
        String userName = AppContext.getInstance().getCurrentUser().getName();
        ArrayList<MatBeanCount> matBeen = (ArrayList<MatBeanCount>) mDaoSession.getMatBeanCountDao().queryBuilder().where(MatBeanCountDao.Properties.Type.eq(type),MatBeanCountDao.Properties.UserName.eq(userName),MatBeanCountDao.Properties.HeadId.eq(headId)).list();
        return matBeen;
    }


    public void updateFDCStock(String id,String name){
        MatBeanCount matBeanCount = new MatBeanCount();
        matBeanCount.setFDCStockID(id);
        matBeanCount.setFDCStockName(name);
        mDaoSession.getMatBeanCountDao().update(matBeanCount);
    }


    public void updateMat(MatBeanCount bean) {
        MatBeanCountDao matBeanDao = mDaoSession.getMatBeanCountDao();
        matBeanDao.update(bean);
    }

    public void delAllMatByHeadId(long headId){
        String name = AppContext.getInstance().getCurrentUser().getName();
        MatBeanCountDao matBeanDao = mDaoSession.getMatBeanCountDao();
        List<MatBeanCount> list = matBeanDao.queryBuilder().where(MatBeanCountDao.Properties.UserName.eq(name), MatBeanCountDao.Properties.HeadId.eq(headId)).list();
        matBeanDao.deleteInTx(list);
    }

}
